import request from '@/utils/request'
import upload from '@/utils/upload'
import config from '@/config'
import { getToken } from '@/utils/auth'

// 获取工单列表
export function getRepairList(params) {
  return request({
    url: '/admin-api/door/order/repair/list',
    method: 'get',
    params: params
  })
}
//获取个人列表
export function getRepairList1(params) {
  return request({
    url: '/admin-api/door/order/repair/listByOpera',
    method: 'get',
    params: params
  })
}



// 获取房产资料列表
export function getAdslList(params) {
  return request({
    url: '/door/adsl/list',
    method: 'get',
    params: params
  })
}

// 新增工单
export function addRepair(data) {
  return request({
    url: '/admin-api/door/order/repair/add',
    method: 'post',
    data: data
  })
}

// 上传工单图片
export function uploadRepairImage(data) {
  return upload({
    url: '/common/upload',
    name: 'file',
    filePath: data.filePath
  })
}

// 获取工单详情
export function getRepairDetail(id) {
  return request({
    url: `/admin-api/door/order/repair/${id}`,
    method: 'get'
  })
}

// 分配工单
export function assignRepair(data) {
  return request({
    url: '/admin-api/door/order/repair/assign',
    method: 'post',
    data: data
  })
}

// 接受工单
export function acceptRepair(data) {
  return request({
    url: '/admin-api/door/order/repair/receive',
    method: 'post',
    data: data
  })
}

// 完成工单
export function completeRepair(data) {
  return request({
    url: '/admin-api/door/order/repair/complete',
    method: 'post',
    data: data
  })
}

// 上传图片
export function uploadImage(filePath) {
  return new Promise((resolve, reject) => {
    uni.uploadFile({
      url: config.baseUrl + '/common/upload',
      filePath: filePath,
      name: 'file',
      header: {
        Authorization: 'Bearer ' + getToken()
      },
      success: (uploadRes) => {
        try {
          const data = JSON.parse(uploadRes.data);
          if (data.code === 200) {
            resolve(data.url || data.fileName);
          } else {
            reject(new Error(data.msg || '上传失败'));
          }
        } catch (e) {
          reject(new Error('解析上传结果失败'));
        }
      },
      fail: () => {
        reject(new Error('上传请求失败'));
      }
    });
  });
} 